#ifndef VECTOR_TABLE_H
#define VECTOR_TABLE_H

template <typename _Type>
class VectorTable
{
    VectorTable(VectorTable &);
    VectorTable &operator=(VectorTable &);
public:
    typedef _Type*  type_ptr;
    typedef _Type  type_node;
    VectorTable()
        : mArray(nullptr), counts(0), ref(0)
    {}
    ~VectorTable()
    {
        delete [] mArray;
    }

    type_node &operator [] (int i)
    {
        return mArray[i];
    }
    type_node &operator [] (int i) const
    {
        return mArray[i];
    }

    void push_back(type_node node)
    {
        if (ref == counts)
        {
            reallocVector();
        }

        mArray[ref++] = node;
    }

    int  size() const
    {
        return ref;
    }

    /**
     * @brief pop_back
     * @return
     * @warning 使用之前必须判断数组是否为空，只有在！empty()时候才能调用
     */
    type_node pop_back()
    {
        return mArray[--ref];
    }
    /**
     * @brief back
     * @return
     * @warning 使用之前必须判断数组是否为空，只有在！empty()时候才能调用
     */
    type_node back()
    {
        return mArray[ref-1];
    }
    /**
     * @brief front
     * @return
     * @warning 使用之前必须判断数组是否为空，只有在！empty()时候才能调用
     */
    type_node front()
    {
        return mArray[ref-1];
    }

    void clear()
    {
        ref = 0;
    }

    void clearPtr()
    {
        for (int i = 0; i < ref; ++i)
        {
            delete mArray[i];
        }
        ref = 0;
    }

    bool empty() const
    {
        return 0 == ref;
    }

private:
    void reallocVector()
    {
        counts += 20;

        type_node *arr = new type_node[counts];

        if (mArray)
        {
            for (int i = 0; i < ref; ++i)
            {
                arr[i] = mArray[i];
            }
        }
        mArray = arr;
    }

    type_ptr    mArray;
    int         counts;
    int         ref;
};


#endif // VECTOR_TABLE_H
